<!-- extend from base layout -->
{% extends "layout.html" %}

{% block extra_css %}

{% endblock %}

{% block content %}
    <div class="container-fluid">
    <ol class="breadcrumb">
        <li><a href="/">后台管理</a></li>
        <li><a href="javascript:void(0);" onclick="$('#menu-order').trigger('click');">订单管理</a></li>
        <li class="active">提现匹配</li>
    </ol>
{#    <h2 class="sub-header">Apply Get Matcher!</h2>#}
    <section id="apply_put_info">
        <div class="panel panel-default">
        <div class="panel-heading">
            <strong class="panel-title">提现申请详情
                {% if apply_get_info.money_apply > apply_get_info.money_order %}
                <i>[ 待匹配金额：{{ apply_get_info.money_apply - apply_get_info.money_order }} ]</i>
                {% endif %}
            <span class="glyphicon glyphicon-cog pull-right"></span>
            </strong>
        </div>
        <table class="table">
            <thead>
                <tr>
                    <th>提现ID</th>
                    <th>用户ID</th>
                    <th>用户名称</th>
                    <th>申请类型</th>
                    <th>申请金额</th>
                    <th>订单金额</th>
                    <th>申请状态</th>
                    <th>订单状态</th>
                    <th>删除状态</th>
                    <th>申请时间</th>
                    <th>是否允许拆分</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>{{ apply_get_info.id }}</td>
                    <td>{{ apply_get_info.user_id }}</td>
                    <td>{{ apply_get_info.user_id | nickname }}</td>
                    <td>{{ apply_get_info.type_apply | type_apply }}</td>
                    <td>{{ apply_get_info.money_apply }}</td>
                    <td>{{ apply_get_info.money_order }}</td>
                    <td>{{ apply_get_info.status_apply | status_apply }}</td>
                    <td>{{ apply_get_info.status_order | status_order }}</td>
                    <td>{{ apply_get_info.status_delete | status_delete }}</td>
                    <td>{{ moment(apply_get_info.create_time).format('YYYY-MM-DD HH:mm:ss') }}</td>
                    <td>
                        <label><input title="选择" type="checkbox" id="apply_get_accept_split" value="1" /> 允许拆分</label>
                    </td>
                </tr>
            </tbody>
        </table>
        </div>
    </section>
    {% if apply_get_info.status_order > STATUS_ORDER_HANDING | int %}
    <section id="apply_match_get_info">
        <div class="panel panel-default">
        <div class="panel-heading">
            <strong class="panel-title">匹配订单列表
            <span class="glyphicon glyphicon-cog pull-right"></span>
            </strong>
        </div>
        <table class="table table-hover table-responsive">
            <thead>
                <tr>
                    <th>订单ID</th>
                    <th>用户ID</th>
                    <th>用户名称</th>
                    <th>订单类型</th>
                    <th>订单金额</th>
                    <th>审核状态</th>
                    <th>支付状态</th>
                    <th>收款状态</th>
                    <th>删除状态</th>
                    <th>订单时间</th>
                </tr>
            </thead>
            <tbody>
                {% for order_info, apply_put_info in apply_put_list %}
                <tr>
                    <td>{{ order_info.id }}</td>
                    <td>{{ order_info.apply_put_uid }}</td>
                    <td>{{ order_info.apply_put_uid | nickname }}</td>
                    <td>{{ order_info.type | type_order }}</td>
                    <td>{{ order_info.money }}</td>
                    <td>{{ order_info.status_audit | status_audit }}</td>
                    <td>{{ order_info.status_pay | status_pay }}</td>
                    <td>{{ order_info.status_rec | status_rec }}</td>
                    <td>{{ order_info.status_delete | status_delete }}</td>
                    <td>{{ moment(order_info.create_time).format('YYYY-MM-DD HH:mm:ss') }}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        </div>
    </section>
    {% endif %}

    {% if apply_get_info.status_order < STATUS_ORDER_COMPLETED | int %}
    <section id="apply_get_list">
        <div class="panel panel-default">
        <div class="panel-heading">
            <strong class="panel-title">投资申请列表
            <span class="glyphicon glyphicon-cog pull-right"></span>
            </strong>
        </div>
        <div class="panel-body">
        <form class="form-inline">
{#            <div class="form-group{% if form.apply_get_id.errors %} has-error{% endif %}">#}
{#                {{ form.apply_get_id.label(class="sr-only") }}#}
{#                {{ form.apply_get_id(class="form-control", placeholder="Apply Get Id") }}#}
{#                {% for error in form.apply_get_id.errors %}#}
{#                    <span class="help-block">{{ error }}</span>#}
{#                {% endfor %}#}
{#            </div>#}
{#            <div class="form-group{% if form.user_id.errors %} has-error{% endif %}">#}
{#                {{ form.user_id.label(class="sr-only") }}#}
{#                {{ form.user_id(class="form-control", placeholder="User Id") }}#}
{#                {% for error in form.user_id.errors %}#}
{#                    <span class="help-block">{{ error }}</span>#}
{#                {% endfor %}#}
{#            </div>#}
{#            <div class="form-group{% if form.type_apply.errors %} has-error{% endif %}">#}
{#                {{ form.type_apply.label(class="sr-only") }}#}
{#                {{ form.type_apply(class="form-control", placeholder="申请类型", data_width="fit", data_header="选择类型") }}#}
{#                {% for error in form.type_apply.errors %}#}
{#                    <span class="help-block">{{ error }}</span>#}
{#                {% endfor %}#}
{#            </div>#}
            <div class="form-group{% if form.min_money.errors %} has-error{% endif %}">
                {{ form.min_money.label(class="") }}
                {{ form.min_money(class="form-control", placeholder="min money", type="number", min="100", max="100000", step="100") }}
                {% for error in form.min_money.errors %}
                    <span class="help-block">{{ error }}</span>
                {% endfor %}
            </div>
            <div class="form-group{% if form.max_money.errors %} has-error{% endif %}">
                {{ form.max_money.label(class="") }}
                {{ form.max_money(class="form-control", placeholder="max money", type="number", min="100", max="100000", step="100") }}
                {% for error in form.max_money.errors %}
                    <span class="help-block">{{ error }}</span>
                {% endfor %}
            </div>
            <div class="form-group{% if form.start_time.errors %} has-error{% endif %}">
                {{ form.start_time.label(class="") }}
                {{ form.start_time(class="form-control", placeholder="start_time", type='date') }}
                {% for error in form.start_time.errors %}
                    <span class="help-block">{{ error }}</span>
                {% endfor %}
            </div>
            <div class="form-group{% if form.end_time.errors %} has-error{% endif %}">
                {{ form.end_time.label(class="") }}
                {{ form.end_time(class="form-control", placeholder="end_time", type='date') }}
                {% for error in form.end_time.errors %}
                    <span class="help-block">{{ error }}</span>
                {% endfor %}
            </div>

            <button type="submit" class="btn btn-primary" name="op" value="0">Search</button>
        </form>


        <hr/>
        <form onsubmit="apply_get_match(this)">
        <input type="hidden" name="apply_get_id" value="{{ apply_get_info.id }}">
        <input type="hidden" id="accept_split" name="accept_split" value="">
        <div class="container-fluid">
            <div class="btn-group pull-right" role="group" aria-label="...">
                <button type="button" class="btn btn-default">金额:<i id="amount-money">0</i></button>
                <button type="submit" class="btn btn-success btn-load" data-loading-text="匹配中" autocomplete="off" id="match-submit">匹配</button>
            </div>
        </div>

        <div class="table-responsive">
            {# <table class="table table-striped"> #}
            <table class="table table-hover">
                <thead>
                <tr>
                    <th>提现ID</th>
                    <th>用户ID</th>
                    <th>用户名称</th>
                    <th>申请类型</th>
                    <th>申请金额</th>
                    <th>申请状态</th>
                    <th>订单状态</th>
                    <th>删除状态</th>
                    <th>申请时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for apply_put, user_profile in pagination.items %}
                <tr class="text-muted apply_put_check_tr">
                    <td>{{ apply_put.id }}</td>
                    <td>{{ apply_put.user_id }}</td>
                    <td>{{ user_profile.nickname }}</td>
                    <td>{{ apply_put.type_apply | type_apply }}</td>
                    <td>{{ apply_put.money_apply }}</td>
                    <td>{{ apply_put.status_apply | status_apply }}</td>
                    <td>{{ apply_put.status_order | status_order }}</td>
                    <td>{{ apply_put.status_delete | status_delete }}</td>
                    <td>{{ moment(apply_put.create_time).format('YYYY-MM-DD HH:mm:ss') }}</td>
                    <td>
                        <label><input title="选择" type="checkbox" class="apply_put_check" name="apply_put_id" id="{{ apply_put.money_apply }}" value="{{ apply_put.id }}" /> 选择</label>
                    </td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
            {# 翻页 #}
            {% from "macros.html" import render_pagination %}
            {{ render_pagination(pagination, 'user.lists') }}
        </div>
        </form>
        </div>
        </div>
    </section>
    {% endif %}

    </div>
{% endblock %}

{% block extra_js %}
<script>
    // 初始化赋值
    var type_apply = $('#type_apply');
    type_apply.selectpicker('val', '{{ form.type_apply.data }}');
    // 处理选项修改
    type_apply.on('changed.bs.select', function (event, clickedIndex, newValue, oldValue) {
        // console.log($('#status_lock').val());
        // console.log(event);
        // console.log(clickedIndex);
        // console.log(newValue);
        // console.log(oldValue);
    });

    // 提现匹配
    function apply_get_match(form) {
        //console.log($(form).serialize());
        $.ajax({
            url: "{{ url_for('apply_get.ajax_match') }}",
            type: 'POST',
            data: $(form).serialize(),
            dataType: 'json',
            success: function (result) {
                // console.log(result);
                if(result.hasOwnProperty('error')){
                    alert(result.error);
                }else {
                    alert(result.success);
                }
            }
        });
        //$(form).button('reset');  // 重置表单 恢复保存按钮
        // todo 更新 tr 数据(blog_id)
        return false;
    }
    // 计算金额
    $(function(){
        //数据行单击选中
{#        $('.apply_put_check_tr').click(function () {#}
{#            //console.log($(this).children("td").children("input.apply_put_check"));#}
{#            $(this).children("td").children("input.apply_put_check").trigger("click");#}
{#        });#}
        //选中状态变化同步更新选中金额
        $('.apply_put_check').change(function () {
            var sum=0;
            $("input.apply_put_check:checkbox:checked").each(function(){
                sum+= parseInt(this.id);
            });
            $('#amount-money').html(sum);
        });
        //选中允许拆分
        $('#apply_get_accept_split').change(function () {
            //console.log($(this).prop("checked"));
            if($(this).prop("checked") == true){
                $('#accept_split').val('1');
            }else {
                $('#accept_split').val('');
            }
        });
    });
</script>
{% endblock %}
